/*
 * @Author: WJC
 * @Date: 2018-12-10 20:24:07
 * @Last Modified by: WJC
 * @Last Modified time: 2018-12-11 17:05:26
 */

// 声明全局变量
var case_no;
var pid;
var pname;
var sex;
var birthday;
var age;
var settleType;
var diagStatus;
var case_info;

/**
 * 将不足10位的数值，转换为长度为10的字符串
 * @param {number} num
 */
function getTenStr(num) {
  // 将num转换成字符串，根据字符串长度获得正确的数值位数
  var str = num + "";
  var len = str.length;
  // 根据字符串长度进行位数补全，补全到10位，不足位添0
  if (len < 10) {
    for (var i = 0; i < 10 - len; i++) {
      str = "0" + str;
    }
  }
  return str;
}

/**
 * 根据"出生日期"获得相应的"年龄"
 * @param {string} birth
 */
function getAgeByBirthday(birth) {
  // 对获得的"出生日期"字符串进行拆分，得到年、月、日的字符串
  var _year = birth.substring(0, birth.indexOf("-"));
  var _month = birth.substring(birth.indexOf("-") + 1, birth.lastIndexOf("-"));
  var _date = birth.substring(birth.lastIndexOf("-") + 1, birth.length);
  // 将年、月、日的字符串转换为相应的数值
  var y = parseInt(_year);
  var m = parseInt(_month) - 1;
  var d = parseInt(_date);
  // 获得"出生日期"的时间戳
  var birthTime = new Date(y, m, d).getTime();
  // 获得当前时间的时间戳
  var nowTime = new Date().getTime();
  // 计算出相差的时间
  var c = nowTime - birthTime;

  var age = Math.floor(c / (365 * 24 * 60 * 60 * 1000));
  if (age < 1) {
    age = Math.floor(c / (30 * 24 * 60 * 60 * 1000));
    age += "月";
    if (age < 1) {
      age = Math.floor(c / (24 * 60 * 60 * 1000));
      age += "天";
    }
  } else {
    age += "岁";
  }
  // 返回"年龄"
  console.log(_year + "、" + _month + "、" + _date);
  return age;
}

/**
 * 获得登陆医生旗下的挂号信息
 */
function getPatientInfo() {
  $.ajax({
    type: "post",
    url: "../getRegisterByDoctor",
    //data: "data",
    dataType: "text",
    error: function() {
      alert("获得患者信息的ajax失败");
    },
    success: function(response) {
      var num = parseInt(response);
      var tdTbody = $("#to_diagnose");
      var bdTbody = $("#been_diagnosed");

      // 清空表格
      tdTbody.empty();
      bdTbody.empty();

      if (num == -1) {
        alert("未获得登陆信息");
        return;
      } else if (num == -2) {
        tdTbody.append("<tr><td>没有查询到相应的患者信息</td></tr>");
        bdTbody.append("<tr><td>没有查询到相应的患者信息</td></tr>");
        return;
      } else {
        var json = eval("(" + response + ")");
        var list = json.list;

        for (var i = 0; i < list.length; i++) {
          if (list[i][6] == 1) {
            tdTbody.append(
              "<tr onclick='getInfoByClick(this);'><td class='case_no'>" +
                getTenStr(list[i][0]) +
                "</td><td class='pid' style='display:none;'>" +
                list[i][1] +
                "</td><td class='pname'>" +
                list[i][2] +
                "</td><td class='sex' style='display:none;'>" +
                (list[i][3] == 1 ? "男" : list[i][3] == 2 ? "女" : "其他") +
                "</td><td class='birthday' style='display:none;'>" +
                list[i][4] +
                "</td><td class='age'>" +
                getAgeByBirthday(list[i][4]) +
                "</td><td class='settle_type' style='display:none;'>" +
                list[i][5] +
                "</td><td class='diag_status' style='display:none;'>" +
                "待诊" +
                "</td><td class='case_info' style='display:none;'>" +
                list[i][7] +
                "</td></tr>"
            );
          } else if (list[i][6] == 2) {
            bdTbody.append(
              "<tr onclick='getInfoByClick(this);'><td class='case_no'>" +
                getTenStr(list[i][0]) +
                "</td><td class='pid' style='display:none;'>" +
                list[i][1] +
                "</td><td class='pname'>" +
                list[i][2] +
                "</td><td class='sex' style='display:none;'>" +
                (list[i][3] == 1 ? "男" : list[i][3] == 2 ? "女" : "其他") +
                "</td><td class='birthday' style='display:none;'>" +
                list[i][4] +
                "</td><td class='age'>" +
                getAgeByBirthday(list[i][4]) +
                "</td><td class='settle_type' style='display:none;'>" +
                list[i][5] +
                "</td><td class='diag_status' style='display:none;'>" +
                "已诊" +
                "</td><td class='case_info' style='display:none;'>" +
                list[i][7] +
                "</td></tr>"
            );
          }
        }
      }
    }
  });
}

/**
 * 通过点击表格行，获得行内信息
 * @param {element} obj
 */
function getInfoByClick(obj) {
  var tr = $(obj);
  case_no = tr.children(".case_no").html();
  pid = tr.children(".pid").html();
  pname = tr.children(".pname").html();
  sex = tr.children(".sex").html();
  birthday = tr.children(".birthday").html();
  age = tr.children(".age").html();
  settleType = tr.children(".settle_type").html();
  diagStatus = tr.children(".diag_status").html();
  caseInfo = tr.children(".case_info").html();

  $("#red").css("color", "black");
  $("#red").html(
    "<span style='font-weight:bolder'>就诊状态：</span><span style='color:green;'>" +
      diagStatus +
      "</span>&nbsp;&nbsp;<span style='font-weight:bolder'>就诊号：</span>" +
      case_no +
      "&nbsp;&nbsp;<span style='font-weight:bolder'>姓名：</span>" +
      pname +
      "&nbsp;&nbsp;<span style='font-weight:bolder'>性别：</span>" +
      sex +
      "&nbsp;&nbsp;<span style='font-weight:bolder'>年龄：</span>" +
      age +
      "&nbsp;&nbsp;<span style='font-weight:bolder'>结算类别：</span>" +
      settleType
  );

  $(".tname").html(pname);
  $(".tcase_no").html(case_no);
  $(".tsex").html(sex);
  $(".tbirthday").html(birthday);
  $(".tage").html(age);
  $(".tpid").html(pid);
  $(".tcase_text").html(caseInfo);
}

/**
 * 向服务器端发送病例信息
 */
function sendCaseText() {
  $.ajax({
    type: "post",
    url: "../caseinfo",
    data: {
      caseNo: case_no,
      caseText: $("#case_text").val()
    },
    dataType: "text",
    error: function() {
      alert("发送病例ajax失败");
    },
    success: function(response) {
      var num = parseInt(response);
      if (num == 1) {
        alert("提交成功");
        return;
      } else if (num == -1) {
        alert("提交失败");
        return;
      } else if (num == -2) {
        alert("已存在，无法提交");
        return;
      }
    }
  });
}

/**
 * 根据病历号或姓名搜索患者
 */
function searchPatientInfo() {
  $.ajax({
    type: "post",
    url: "../searchRegister",
    data: {
      searchInfo: $("input[name='search_info']").val()
    },
    dataType: "text",
    error: function() {
      alert("搜索的ajax失败");
    },
    success: function(response) {
      var num = parseInt(response);
      var tdTbody = $("#to_diagnose");
      var bdTbody = $("#been_diagnosed");

      // 清空表格
      tdTbody.empty();
      bdTbody.empty();

      if (num == -1) {
        alert("未获得登陆信息");
        return;
      } else if (num == -2) {
        tdTbody.append("<tr><td>没有查询到相应的患者信息</td></tr>");
        bdTbody.append("<tr><td>没有查询到相应的患者信息</td></tr>");
        return;
      } else {
        var json = eval("(" + response + ")");
        var list = json.list;

        for (var i = 0; i < list.length; i++) {
          if (list[i][6] == 1) {
            tdTbody.append(
              "<tr onclick='getInfoByClick(this);'><td class='case_no'>" +
                getTenStr(list[i][0]) +
                "</td><td class='pid' style='display:none;'>" +
                list[i][1] +
                "</td><td class='pname'>" +
                list[i][2] +
                "</td><td class='sex' style='display:none;'>" +
                (list[i][3] == 1 ? "男" : list[i][3] == 2 ? "女" : "其他") +
                "</td><td class='birthday' style='display:none;'>" +
                list[i][4] +
                "</td><td class='age'>" +
                getAgeByBirthday(list[i][4]) +
                "</td><td class='settle_type' style='display:none;'>" +
                list[i][5] +
                "</td><td class='diag_status' style='display:none;'>" +
                "待诊" +
                "</td><td class='case_info' style='display:none;'>" +
                list[i][7] +
                "</td></tr>"
            );
          } else if (list[i][6] == 2) {
            bdTbody.append(
              "<tr onclick='getInfoByClick(this);'><td class='case_no'>" +
                getTenStr(list[i][0]) +
                "</td><td class='pid' style='display:none;'>" +
                list[i][1] +
                "</td><td class='pname'>" +
                list[i][2] +
                "</td><td class='sex' style='display:none;'>" +
                (list[i][3] == 1 ? "男" : list[i][3] == 2 ? "女" : "其他") +
                "</td><td class='birthday' style='display:none;'>" +
                list[i][4] +
                "</td><td class='age'>" +
                getAgeByBirthday(list[i][4]) +
                "</td><td class='settle_type' style='display:none;'>" +
                list[i][5] +
                "</td><td class='diag_status' style='display:none;'>" +
                "已诊" +
                "</td><td class='case_info' style='display:none;'>" +
                list[i][7] +
                "</td></tr>"
            );
          }
        }
      }
    }
  });
}

function bindEvent() {
  // 刷新按钮
  $(".refresh_btn").click(function() {
    getPatientInfo();
  });

  // 门诊病例
  $(".case").click(function() {
    if (case_no == undefined || case_no == null || case_no == "") {
      alert("请选择患者");
      return;
    }
    $("#myModal").modal("toggle");
  });

  // 病例提交按钮
  $(".put_in").click(function() {
    // 判断输入内容
    if ($("#case_text").val() == "" || $("#case_text").val() == null) {
      alert("内容不可以为空");
      return;
    }
    sendCaseText();
  });

  // 搜索按钮
  $(".search_btn").click(function() {
    searchPatientInfo();
  });
}

$(document).ready(function() {
  getPatientInfo();
  bindEvent();
});
